package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys_role")
public class RoleController {
    //DUBBO注入业务层
    @Reference
    private RoleService roleService;
    /**
     * 分页查询角色数据
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping(path = "/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = roleService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_Role_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_Role_FAIL);
        }
    }

    /**
     * 查询角色所有权限信息
     * @return
     */
    @GetMapping("/findAllPermission")
    public Result findAllPermission(){
        try {
            List<Permission> allPermission = roleService.findAllPermission();
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,allPermission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 查询所有菜单信息
     * @return
     */
    @GetMapping("/findAllMenu")
    public Result findAll(){
        try {
            List<Menu> allMenu = roleService.findAllMenu();
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,allMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    /**
     * 新增角色
     * @param role
     * @return
     */
    @RequestMapping(path = "/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[]menuIds){
        try {
            roleService.add(role,permissionIds,menuIds);
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /**
     * 根据id查询角色信息详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    /**
     * 查询指定角色关联的权限id
     * @param roleId
     * @return
     */
    @GetMapping("/{id}/permissions")
    public Result findRoleAssociationIds(@PathVariable("id") Integer roleId){
        try {
            Integer[] permissionAssociationIds = roleService.findPermissionAssociationIds(roleId);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionAssociationIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
    /**
     * 查询指定角色关联的菜单id
     * @param roleId
     * @return
     */
    @GetMapping("/{id}/menus")
    public Result findMenuAssociationIds(@PathVariable("id") Integer roleId){
        try {
            Integer[] menuAssociationIds = roleService.findMenuAssociationIds(roleId);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menuAssociationIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }


     /**
     *  根据权限Id及菜单ID更新角色基本信息
     * @param role
     * @param permissionIds
     * @param menuIds
     * @return
     */
    @PutMapping("/updata")
    public Result update(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.update(role, permissionIds,menuIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_ROLE_FAIL);
        }
    }
    /**
     * 根据ID删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            roleService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
